<template>
  <div class="list" ref="list">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <span class="button">{{this.currentCity}}</span>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <span class="button"
                :key="item.id"
                v-for="item in hotCities"
                @click="handleCityClick(item.name)"
          >{{item.name}}</span>
        </div>
      </div>
      <div class="area">
        <div v-for="(item,key) in cities" :key="key" :ref="key">
          <div class="title border-topbottom">{{key}}</div>
          <ul class="city-list">
            <li class="city border-bottom"
                v-for="city in item"
                :key="city.id"
                @click="handleCityClick(city.name)"
            >
              {{city.name}}
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import { mapState, mapMutations } from 'vuex'
  import BScroll from 'better-scroll'

  export default {
    props: {
      cities: Object,
      hotCities: Array,
      text: ''
    },
    mounted () {
      this.scroll = new BScroll(this.$refs.list)
    },
    computed: {
      ...mapState({currentCity: 'city'})
    },
    methods: {
      handleCityClick (cityName) {
        this.changeCity(cityName)
        this.$router.push('/')
      },
      ...mapMutations(['changeCity'])
    },
    watch: {
      text () {
        if (this.text) {
          this.scroll.scrollToElement(this.$refs[this.text][0])
        }
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc

  .border-bottom
    &:before
      border-color: #ccc

  .list
    position absolute
    top 1.58rem
    left 0
    right 0
    bottom 0
    overflow hidden
    .title
      padding-left: .2rem
      line-height: .58rem
      color #666
      background: #eee
      font-size: .26rem
    .button-list
      padding .1rem .6rem .1rem .1rem
      .button
        display inline-block
        width: 28%
        margin: .1rem
        padding: .1rem 0
        text-align center
        border: .02rem solid #ccc
        border-radius: .06rem
    .city-list
      .city
        padding-left .2rem
        line-height: .76rem


</style>
